/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.*;
import com.ipartek.formacion.gestformwww.modelo.beans.*;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Alvaro y Sara
 */
public class ListarMatriculasAccion implements Accion{
  
    private final String visualizarcurso = "visualizar-curso-accion";
    private final String listarcursos = "listar-cursos-accion";
/**
 * Devuelve la lista de cursos en los que esta matriculado un alumno
 * @param servlet
 * @param request
 * @param response
 * @return el router
 */
    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion;
         AccionServlet as=(AccionServlet) servlet;
        sesion = request.getSession();
        AccionRouter router=new AccionRouter("principal");

        Integer idAlumno = -1;
        Integer idCurso = -1;

        ModeloAlumno malumno = (ModeloAlumno) servlet.getServletContext().getAttribute("ModeloAlumno");
        ModeloCurso mcurso = (ModeloCurso) servlet.getServletContext().getAttribute("ModeloCurso");
        int numMatriculas = -1;

        ArrayList<Curso> cursos = new ArrayList<Curso>();

        if ((Verificador.numeroValido(request.getParameter("idAlumno")) || (request.getAttribute("idAlumno") != null))&&
                (Verificador.numeroValido(request.getParameter("idCurso")) || (request.getAttribute("idCurso") != null))){
                if (request.getAttribute("idAlumno") != null) {
                    idAlumno = (Integer) request.getAttribute("idAlumno");
                } else {
                    idAlumno = Integer.parseInt(request.getParameter("idAlumno"));
                }
                if (request.getAttribute("idCurso") != null) {
                    idCurso = (Integer) request.getAttribute("idCurso");
                } else {
                    idCurso = Integer.parseInt(request.getParameter("idCurso"));
                }
            try {
                cursos = mcurso.listarCursosDeAlumno(idAlumno);
            } catch (GestorFormacionExcepcion ex) {
                Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                 sesion.setAttribute("excepciones", ex.getMessage());
            }
                try {
                numMatriculas = malumno.cuentaMatricula(idAlumno);
            } catch (GestorFormacionExcepcion ex) {
                Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                 sesion.setAttribute("excepciones", ex.getMessage());
            }

                if (cursos!=null){
                    request.setAttribute("listacursos", cursos);
                    request.setAttribute("numeromatriculas", numMatriculas);
                    request.setAttribute("contenido-alumno", "matricula-alumno");
                }
                else{
                    if(request.getParameter("idCurso")!=null){
                        request.setAttribute("idCurso",request.getParameter("idCurso"));
                    try {
                        router=as.getFactory().getAction(visualizarcurso,servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }
                  
                    }else{
                    try {
                        router=as.getFactory().getAction(listarcursos,servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
                    }
                      
                    }
                 }
        }
        else{
            try {
                router=as.getFactory().getAction(listarcursos,servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(ListarMatriculasAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return router;
    }

    public void inicializa(AccionFactory factory) {

    }

}
